package com.tx.admin.detailmodearchive.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tx.admin.detailmodearchive.service.DetailModeArchiveService;
import com.tx.admin.detailmodearchive.pojo.po.DetailModeArchive;
import com.tx.admin.detailmodearchive.mapper.DetailModeArchiveMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.tx.core.common.enums.CommonResponseEnum;
import com.tx.core.util.PageUtils;
import com.tx.core.util.BeanCopyUtils;
import com.tx.core.util.Utils;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.tx.admin.detailmodearchive.pojo.dto.DetailModeArchiveCreateDTO;
import com.tx.admin.detailmodearchive.pojo.dto.DetailModeArchiveUpdateDTO;
import com.tx.admin.detailmodearchive.pojo.dto.DetailModeArchiveListDTO;
import com.tx.admin.detailmodearchive.pojo.dto.DetailModeArchiveImportDTO;
import com.tx.core.common.entity.ImportExcelDTO;
import com.tx.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.tx.core.util.FileUtils;
import com.tx.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.tx.admin.detailmodearchive.pojo.vo.DetailModeArchiveVO;

/**
 * <p>
 * 详情页模版档案信息项 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-05
 */
@Service
@RequiredArgsConstructor
public class DetailModeArchiveServiceImpl extends ServiceImpl<DetailModeArchiveMapper, DetailModeArchive> implements DetailModeArchiveService {
    @Override
    public DetailModeArchive create(DetailModeArchiveCreateDTO dto){
        DetailModeArchive detailModeArchive = BeanCopyUtils.copy(dto, DetailModeArchive.class);
        save(detailModeArchive);
        return detailModeArchive;
    }

    @Override
    public void update(DetailModeArchiveUpdateDTO dto){
        DetailModeArchive detailModeArchive = BeanCopyUtils.copy(dto, DetailModeArchive.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(DetailModeArchive::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(detailModeArchive);
    }

    @Override
    public PageResult<DetailModeArchiveVO> page(DetailModeArchiveListDTO dto){
        Page<DetailModeArchiveVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), DetailModeArchiveVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<DetailModeArchiveVO> list(DetailModeArchiveListDTO dto){
        return listAs(buildQueryWrapper(dto), DetailModeArchiveVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public DetailModeArchiveVO detail(Object id){
        DetailModeArchive detailModeArchive = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(detailModeArchive);
        return BeanCopyUtils.copy(detailModeArchive, DetailModeArchiveVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<DetailModeArchiveImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), DetailModeArchiveImportDTO.class, true);
        List<DetailModeArchiveImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(DetailModeArchiveListDTO dto, HttpServletResponse response) {
        List<DetailModeArchiveVO> list = list(dto);
        String fileName = "详情页模版档案信息项模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "详情页模版档案信息项", DetailModeArchiveVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(DetailModeArchiveListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(DetailModeArchive.class);
        if (Utils.isNotNull(dto.getBelongId())) {
            wrapper.eq(DetailModeArchive::getBelongId, dto.getBelongId());
        }
        if (Utils.isNotNull(dto.getKeyDes())) {
            wrapper.eq(DetailModeArchive::getKeyDes, dto.getKeyDes());
        }
        if (Utils.isNotNull(dto.getValueDes())) {
            wrapper.eq(DetailModeArchive::getValueDes, dto.getValueDes());
        }
        if (Utils.isNotNull(dto.getExt())) {
            wrapper.eq(DetailModeArchive::getExt, dto.getExt());
        }
        if (Utils.isNotNull(dto.getExt1())) {
            wrapper.eq(DetailModeArchive::getExt1, dto.getExt1());
        }
        if (Utils.isNotNull(dto.getExt2())) {
            wrapper.eq(DetailModeArchive::getExt2, dto.getExt2());
        }
        if (Utils.isNotNull(dto.getExt3())) {
            wrapper.eq(DetailModeArchive::getExt3, dto.getExt3());
        }
        if (Utils.isNotNull(dto.getExt4())) {
            wrapper.eq(DetailModeArchive::getExt4, dto.getExt4());
        }
        if (Utils.isNotNull(dto.getExt5())) {
            wrapper.eq(DetailModeArchive::getExt5, dto.getExt5());
        }
        if (Utils.isNotNull(dto.getExt6())) {
            wrapper.eq(DetailModeArchive::getExt6, dto.getExt6());
        }
        if (Utils.isNotNull(dto.getExt7())) {
            wrapper.eq(DetailModeArchive::getExt7, dto.getExt7());
        }
        return wrapper;
    }
}